package org.a;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;



public class Test2 {
	static String sql="\r\n" + 
			"\r\n" + 
			"\r\n" + 
			"nNoticeOrderList - ==>  Preparing: select * from ( select tmp_page.*, rownum row_id from ( select notice_order.id, notice_order.tf_notice_order_no as notice_order_no, notice_order.tf_order_id as order_id, notice_order.tf_order_no as order_no, notice_order.tran_load_id, notice_order.tran_load_no, notice_order.sto_opereate_type, notice_order.confirm_remark confirmRemark, notice_order.confirm_weight confirmWeight, notice_order.pre_storage_time preStorageTime, tf_order.erp_order_no, tf_order.order_type as order_type_id, (select const_name from bas_const where const_code=tf_order.order_type AND const_type=?) as order_type, tf_order.buss_type as buss_type_id, (select const_name from bas_const where const_code=tf_order.buss_type AND const_type=?) as buss_type, tf_order.order_status as order_status_id, (select const_name from bas_const where const_code=tf_order.order_status AND const_type=?) as order_status, notice_order.position_status as position_status_id, (select const_name from bas_const where const_code = notice_order.position_status AND const_type = ?) as position_status, notice_order.order_status as notice_order_status_id, (select const_name from bas_const where const_code=notice_order.order_status AND const_type=?) as notice_order_status, notice_order.display_order_status as display_notice_order_status, (select const_name from bas_const where const_code=notice_order.display_order_status AND const_type=?) as display_nod_status_name, tf_order.customer_id, (select customer_name from bas_customer where id=tf_order.customer_id) as customer_name, tf_order.company_id, (select company.const_name from bas_const company where company.const_type='Company' and company.const_code = tf_order.company_id) as company_name, tf_order.sto_in_factory_id as in_factory_id, (select factory_name from bas_factory_info where id=tf_order.sto_in_factory_id) as in_factory_name, tf_order.sto_out_factory_id as out_factory_id, (select factory_name from bas_factory_info where id=tf_order.sto_out_factory_id) as out_factory_name, tf_order.supplier_id, (select customer_name from bas_customer where id=tf_order.supplier_id) as supplier_name, tf_order.product_workshop_id as whorkshop_id, (select workshop_name from bas_workshop where id=tf_order.product_workshop_id) as workshop_name, case tf_order.is_frozen when 1 then '是' else '否' end as is_frozen, notice_order.driver_name, notice_order.car_no, notice_order.contact_phone, tf_order.remark, notice_order.add_user_name, notice_order.add_time, notice_order.audit_user_id, notice_order.audit_user_name, case notice_order.audit_state when 0 then '未审核' else '审核' end as audit_state, notice_order.audit_time, notice_order.in_wh_id, notice_order.product_team as product_team_id, notice_order.CONFIRM_WEIGHT as receive_total_weight, tf_order.customer_marking_no, tf_order.sale_dealer_id, (select customer_name from bas_customer where id=tf_order.sale_dealer_id) as sale_dealer_name, tf_order.tra_reason, bwhOut.ware_house_name outWareHouseName, bwhIn.ware_house_name inWareHouseName, tf_order.OMS_ORDER_NO AS OMS_ORDER_NO, notice_order.sap_account_no, notice_order.sap_no, notice_order.unit_group, (select const_name from bas_const where const_code=notice_order.unit_group AND const_type='UNIT_GROUP') as unitGroupStr, (select const_name from bas_const where const_code=notice_order.product_team AND const_type='Shift') as product_team, tf_order.requisition_recipient, (select collar_name from bas_collar where id=tf_order.requisition_recipient) as requisition_recipient_name, case when tf_order.STO_OPEREATE_TYPE = 0 then tf_order.STO_IN_FACTORY_ID else tf_order.STO_OUT_FACTORY_ID end as FACTORY_ID from sto_tf_notice_order notice_order inner join sto_tf_order tf_order on notice_order.tf_order_id = tf_order.id left join bas_ware_house bwhOut on bwhOut.id=tf_order.sto_out_wh_id left join bas_ware_house bwhIn on bwhIn.id=tf_order.sto_in_wh_id left join ( select distinct job.tf_notice_order_id from sto_tf_job job WHERE JOB .is_deleted = 0 )job on job.tf_notice_order_id = notice_order.id WHERE notice_order.is_deleted=0 AND notice_order.sto_opereate_type = ? AND EXISTS(SELECT 1 FROM bas_const const2 WHERE const2.const_code=notice_order.order_status AND const2.const_type=? AND const2.enums_value IN (2, 3, 4, 5)) ORDER BY notice_order.add_time DESC ) tmp_page where rownum <= ? ) where row_id > ? \r\n" + 
			"2019-04-22 10:53:23.056 [pc_328] DEBUG c.h.s.w.p.d.S.getInNoticeOrderList - ==> Parameters: STO_ASN_ORDER_ORDER_TYPE(String), STO_ASN_ORDER_BUSS_TYPE(String), STO_ASN_ORDER_ORDER_STATUS(String), STO_POSITION_STATUS(String), STO_ASN_NOTICE_ORDER_ORDER_STATUS(String), STO_ASN_NOTICE_ORDER_DISPLAY_ORDER_STATUS(String), 0(Integer), STO_ASN_NOTICE_ORDER_ORDER_STATUS(String), 5(Integer), 0(Integer)";
	public static void main(String[] args) throws IOException {
		String sql1 = sql.split("Preparing:")[1];
		
		String[] as = sql1.split("\\r\\n|\\s+[\\s\\d-:\\.\\[]+.*==> Parameters:");
		System.out.println(as[0]);
		System.out.println(as[1]);
		System.out.println("-----------------------------");
		System.out.println(appSql(as[0], as[1]));
		/*InputStream in =null;
		try {
				
			Properties properties = new Properties();
	    // 使用ClassLoader加载properties配置文件生成对应的输入流
	     in = Test2.class.getClassLoader().getResourceAsStream("a.properties");
	    // 使用properties对象加载输入流
	    properties.load(in);
	    //获取key对应的value值
		String newsql = appSql(properties.getProperty("sql"), properties.getProperty("param"));
		System.out.println(newsql);
		} catch (Exception e) {
			if(in!=null) {
				in.close();
			}
		}
		*/
	}
	private static String appSql(String sql,String param) {
		List<String> ps= Arrays.asList(param.split(","));
		char[] sqls=sql.toCharArray();
		int i=0;
		StringBuffer newSql=new StringBuffer();
		for(char s:sqls) {
			String n=String.valueOf(s);
			if(s=='?') {
				String p=ps.get(i++) ;
				n=p.replaceAll("\\(.*\\)","");
				if(p.contains("String")) {
					n="'"+n.trim()+"'";
				}
				
				
			}
			newSql.append(n );
			
		}
		
		return newSql.toString();
	}
}
